package com.luck.easyfoodbatch.ui;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.luck.easyfoodbatch.App;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.adapter.ShopAdapter;
import com.luck.easyfoodbatch.mvp.home.RecommendGoodsModel;
import com.luck.easyfoodbatch.mvp.home.RecommendGoodsPresenter;
import com.luck.easyfoodbatch.mvp.home.RecommendGoodsView;
import com.luck.easyfoodbatch.mvp.model.SearchGoods;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.SharedPreferencesUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Utils;
import com.luck.easyrecyclerview.EasyRecyclerView;
import com.luck.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.luck.easyrecyclerview.decoration.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ShopDataListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, RecyclerArrayAdapter.OnLoadMoreListener, RecommendGoodsView {

    @BindView(R.id.recyclerView)
    EasyRecyclerView recyclerView;
    private ShopAdapter adapter;
    private List<SearchGoods.DataBean.ModelsBean> list = new ArrayList<>();
    private RecommendGoodsModel goodsModel;
    private int totalPage = 1;
    private int page = 1;
    private boolean isRefresh = false;
    private String p_id = "3";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_data_list, "好食推荐", MODE_BACK_NAVIGATION);
        App.getInstance().addActivity(ShopDataListActivity.this);
        ButterKnife.bind(this);
        initViews();
    }

    /**
     * 初始化
     */
    private void initViews() {
        goodsModel = new RecommendGoodsPresenter(this, mContext);
        p_id = (String) SharedPreferencesUtil.getData(ShopDataListActivity.this, HttpUtil.P_ID, "3");
        showProgressDialog(getString(R.string.app_loading));
        goodsModel.requestRecommendGoods(page, p_id);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.HORIZONTAL, Utils.dip2px(mContext, 0.5F), getResources().getColor(R.color.line_color)));
        recyclerView.setLayoutManager(manager);
        adapter = new ShopAdapter(mContext);
        adapter.setMore(R.layout.view_more, this);
        adapter.setNoMore(R.layout.view_nomore);
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                SearchGoods.DataBean.ModelsBean bean = list.get(position);
                Bundle bundle = new Bundle();
                bundle.putString("gid", bean.getGid() + "");
                startAct(ShopDetailsActivity.class, bundle);
            }
        });
        adapter.addAll(list);
        recyclerView.setAdapter(adapter);
        recyclerView.setRefreshListener(this);
    }

    @Override
    public void onRefresh() {
        isRefresh = true;
        page = 1;
        goodsModel.requestRecommendGoods(page, p_id);
    }

    @Override
    public void onLoadMore() {
        //刷新
        isRefresh = false;
        page++;
        if (!isNetworkConnected()) {
            adapter.pauseMore();
            return;
        }
        if (page > totalPage) {
            adapter.stopMore();
            return;
        }
        goodsModel.requestRecommendGoods(page, p_id);
    }

    @Override
    public void getRecommendGoodsSuccess(SearchGoods model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                SearchGoods.DataBean data = model.getData();
                if (isRefresh) {
                    list.clear();
                    adapter.clear();
                    isRefresh = false;
                }
                list.addAll(data.getModels());
                adapter.addAll(list);
                adapter.notifyDataSetChanged();
            } else {
                T.showToast(mContext, model.getMsg());
            }
        }
    }

    @Override
    public void httpStatusError() {
        T.showToast(mContext, getString(R.string.app_http_error));
    }

    @Override
    public void hideLoading() {
        hideShowProgress();
    }
}
